package com.thm.tfast.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.thm.tfast.core.enums.DeleteFlagEnum;
import com.thm.tfast.core.exception.Asserts;
import com.thm.tfast.core.sevruity.domain.SysUserDetails;
import com.thm.tfast.core.utils.SecurityUtil;
import com.thm.tfast.modules.sys.model.SysPermission;
import com.thm.tfast.modules.sys.mapper.SysPermissionMapper;
import com.thm.tfast.modules.sys.model.SysRolePermission;
import com.thm.tfast.modules.sys.model.dto.permission.PermissionInsertDto;
import com.thm.tfast.modules.sys.model.dto.permission.PermissionUpdateDto;
import com.thm.tfast.modules.sys.model.vo.permission.PermissionVo;
import com.thm.tfast.modules.sys.service.SysCacheService;
import com.thm.tfast.modules.sys.service.SysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thm.tfast.modules.sys.service.SysRolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 权限信息表 服务实现类
 * </p>
 *
 * @author THM
 * @since 2022-10-25
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Resource
    private SysCacheService sysCacheService;
    @Resource
    private SysRolePermissionService sysRolePermissionService;
    @Override
    public List<PermissionVo> getPermissionByUserId(Long userId) {
        List<SysPermission> sysPermissions = this.baseMapper.getPermissionByUserId(userId);
        return BeanUtil.copyToList(sysPermissions,PermissionVo.class);
    }

    @Override
    @Transactional
    public Long insert(PermissionInsertDto dto) {
        SysPermission sysPermission = this.getOne(Wrappers
                .<SysPermission>lambdaQuery()
                .eq(SysPermission::getPermission, dto.getPermission()));
        if (Objects.nonNull(sysPermission)){
            Asserts.fail("权限标识已存在");
        }
        sysPermission = BeanUtil.toBean(dto,SysPermission.class);
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysPermission.setCreateUser(currentUser.getUserId());
        sysPermission.setCreateTime(currentUser.getDate());
        sysPermission.setUpdateUser(currentUser.getUserId());
        sysPermission.setUpdateTime(currentUser.getDate());
        this.save(sysPermission);
        return sysPermission.getId();
    }

    @Override
    @Transactional
    public Boolean update(PermissionUpdateDto dto) {
        if (Objects.isNull(this.getById(dto.getId()))){
            Asserts.fail("权限信息不存在");
        }
        SysPermission sysPermission = this.getOne(Wrappers
                .<SysPermission>lambdaQuery()
                .eq(SysPermission::getPermission, dto.getPermission()));
        if (Objects.nonNull(sysPermission)){
            Asserts.fail("权限标识已存在");
        }
        sysPermission = BeanUtil.toBean(dto,SysPermission.class);
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysPermission.setUpdateUser(currentUser.getUserId());
        sysPermission.setUpdateTime(currentUser.getDate());
        //删除权限相关缓存
        delPermissionCache(dto.getId());
        return this.updateById(sysPermission);
    }

    @Override
    @Transactional
    public Boolean delete(Long permissionId) {
        SysPermission sysPermission = this.getById(permissionId);
        if (Objects.isNull(sysPermission)){
            Asserts.fail("权限信息不存在");
        }
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysPermission.setUpdateUser(currentUser.getUserId());
        sysPermission.setUpdateTime(currentUser.getDate());
        sysPermission.setDelFlag(DeleteFlagEnum.DELETED.getCode());
        //删除权限相关缓存
        delPermissionCache(permissionId);
        return  this.updateById(sysPermission);
    }

    /**
     * 删除角色相关用户权限缓存
     * @param permissionId 权限ID
     */
    private void delPermissionCache(Long permissionId){
        List<SysRolePermission> sysRolePermissions = sysRolePermissionService.list(Wrappers
                .<SysRolePermission>lambdaQuery()
                .eq(SysRolePermission::getPermissionId, permissionId));
        if (CollectionUtil.isNotEmpty(sysRolePermissions)){
            sysRolePermissions.forEach(sysRolePermission -> sysCacheService.delPermissionByRoleId(sysRolePermission.getRoleId()));
        }
    }
}
